package com.hangzhou.xyj.ui.adapter;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.hangzhou.xyj.R;
import com.hangzhou.xyj.manager.IntentManager;
import com.hangzhou.xyj.model.DramaModel;
import com.hangzhou.xyj.model.DynamicModel;
import com.hangzhou.xyj.model.type.EIntentTagType;
import com.hangzhou.xyj.ui.activity.DynamicDetailActivity;
import com.hangzhou.xyj.ui.adapter.base.BaseLoadingAdapter;
import com.hangzhou.xyj.ui.adapter.viewholder.DynamicImgViewHolder;
import com.hangzhou.xyj.ui.adapter.viewholder.DynamicListenViewHolder;
import com.hangzhou.xyj.ui.adapter.viewholder.DynamicTxtViewHolder;
import com.hangzhou.xyj.utils.constants.DataKey;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by neo.duan on 2015/12/28
 * 关注动态adapter
 */
public class FollowDynamicsAdapter extends BaseLoadingAdapter<DynamicModel> {

    private List<DramaModel> dramaModelList = new ArrayList<>();//播放列表
    private Map<Integer, Integer> positionPosMap = new HashMap<>();//显示的列表的位置 与 List中的位置的映射

    public FollowDynamicsAdapter(RecyclerView recyclerView, List<DynamicModel> dataList) {
        super(recyclerView, dataList);
    }

    @Override
    public RecyclerView.ViewHolder onCreateNormalViewHolder(ViewGroup viewGroup, int viewType) {
        View view;
        switch (viewType) {
            case DynamicModel.TYPE_LISTEN:
                view = mLayoutInflater.inflate(R.layout.layout_active_item_listen, viewGroup, false);
                return new DynamicListenViewHolder(mContext, view);
            case DynamicModel.TYPE_TEXT:
                view = mLayoutInflater.inflate(R.layout.layout_active_item_text, viewGroup, false);
                return new DynamicTxtViewHolder(mContext, view);
            case DynamicModel.TYPE_IMG:
            default:
                view = mLayoutInflater.inflate(R.layout.layout_active_item_img, viewGroup, false);
                return new DynamicImgViewHolder(mContext, view);
        }
    }

    @Override
    public void onBindNormalViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
        switch (getItemViewType(position)) {
            case DynamicModel.TYPE_IMG:
                ((DynamicImgViewHolder) viewHolder).setIntentTag(EIntentTagType.TAG_COMMUNITY_FOCUS_DYNAMIC.getValue());
                ((DynamicImgViewHolder) viewHolder).setData(mDataList.get(position));
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, DynamicDetailActivity.class);
                        intent.putExtra(DataKey.INDEX, mDataList.get(position).getId());
                        intent.putExtra(DataKey.TITLE, mDataList.get(position).getUserName());
                        IntentManager.getInstance().goActivity(mContext, intent);
                    }
                });
                break;
            case DynamicModel.TYPE_LISTEN:
                ((DynamicListenViewHolder) viewHolder).setIntentTag(EIntentTagType.TAG_COMMUNITY_FOCUS_DYNAMIC.getValue());
                ((DynamicListenViewHolder) viewHolder).setData(mDataList.get(position));
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        IntentManager.getInstance().goDramaPlayActivity(mContext, dramaModelList, positionPosMap.get(position));
                    }
                });
                break;
            case DynamicModel.TYPE_TEXT:
            default:
                ((DynamicTxtViewHolder) viewHolder).setIntentTag(EIntentTagType.TAG_COMMUNITY_FOCUS_DYNAMIC.getValue());
                ((DynamicTxtViewHolder) viewHolder).setData(mDataList.get(position));
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(mContext, DynamicDetailActivity.class);
                        intent.putExtra(DataKey.INDEX, mDataList.get(position).getId());
                        intent.putExtra(DataKey.TITLE, mDataList.get(position).getUserName());
                        IntentManager.getInstance().goActivity(mContext, intent);
                    }
                });
                break;
        }
    }


    /**
     * 获取播放列表
     *
     * @return
     */
    private void getDramaPlayList() {
        dramaModelList.clear();//先清空
        positionPosMap.clear();//先清空
        for (int i = 0; i < mDataList.size(); i++) {
            DynamicModel model = mDataList.get(i);
            if (model != null && model.getRealShowType() == DynamicModel.TYPE_LISTEN) {//如果是 试听类型的才加入
                DramaModel dramaModel = new DramaModel();
                dramaModel.setId(model.getId());
                dramaModel.setName(model.getSongName());
                dramaModel.setNativeUrl(model.getNativeUrl());
                dramaModel.setAccompanyUrl(model.getAccompanyUrl());
                dramaModel.setLrcUrl(model.getLrcUrl());
                dramaModelList.add(dramaModel);
                positionPosMap.put(i, dramaModelList.size() - 1);
            }
        }
    }

    @Override
    public void load(List<DynamicModel> newDataList) {
        super.load(newDataList);
        getDramaPlayList();
    }

    @Override
    public void update(List<DynamicModel> newDataList) {
        super.update(newDataList);
        getDramaPlayList();
    }

    @Override
    public int getItemViewType(int position) {
        int viewType = super.getItemViewType(position);
        if (viewType == TYPE_LOADING_ITEM) {//加载条目
            return viewType;
        } else {
            return mDataList.get(position).getRealShowType();
        }
    }
}
